package com.lagou.edu.controller;

import com.lagou.edu.pojo.Resume;
import com.lagou.edu.pojo.User;
import com.lagou.edu.service.ResumeService;
import com.lagou.edu.service.UserService;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 用户接口操作类
 * @author 陶肖寅
 * @version V1.0
 * @Package com.lagou.edu.controller
 * @date 2020-02-02 16:46
 * @Copyright © 2020-2021 城云科技(中国)有限公司
 */
@Controller
@RequestMapping()
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    ResumeService resumeService;
    @PostMapping("/submit")
    public String login(Model model, HttpSession session, String username, String password) throws Exception {
        //判断登录是否成功
        User user = userService.getUser(username,password);
        if(user ==null) {
            //用户信息不存在，则跳到error页
            model.addAttribute("msg","用户信息不存在");
            return "error";
        }
        //登录成功后将用户信息保存到session中
        session.setAttribute("user",user);
        System.out.println("存入Session sessionid:信息" + session.getId());
        //登录成功后，跳转到用户列表页面
        List<Resume> resumes = resumeService.queryResumeList();
        model.addAttribute("resumes",resumes);
        return  "resume/list";
    }
}
